#include "syscall.h"
#include "user.h"

#define Dim 	20	/* sum total of the arrays doesn't fit in 
			 * physical memory 
			 */

void matTest();
void sortTest();


int main(){
	int whichTest, matmultNO, sortNO, i;
	Printf("\n---------------------THIS IS TEST FOR PROJECT 3 STEP 3 TEST:-----------------------");
	Printf("\n1->exec multiple matmults");
	Printf("\n2->exec multiple sorts");
	Printf("\n3->fork multiple matmults");
	Printf("\n4->fork multiple sorts");
	Printf("\nPlease enter 1 or 2");
	whichTest = InputInt();
	switch(whichTest){
		case 1:
		Printf("\nPlease input the number of matmults you want to exec\n");
		matmultNO = InputInt();
		for(i=1;i<=matmultNO;i++){
			Printf("\nExecing the %dth matmult....",i);
			Exec("../test/matmult",sizeof("../test/matmult"));
		}
		break;
		
		case 2:
		Printf("\nPlease input the number of sorts you want to exec");
		sortNO = InputInt();
		for(i=1;i<=sortNO;i++){
			Printf("\nExecing the %dth sort....",i);
			Exec("../test/sort",sizeof("../test/sort"));
		}
		break;
		case 3:
		Printf("\nPlease input the number of matmults you want to fork");
		matmultNO = InputInt();
		for(i=1;i<=matmultNO;i++){
			Printf("\nForking the %dth sort....",i);
			Fork(matTest);
		}
		break;
		case 4:
		Printf("\nPlease input the number of sorts you want to fork");
		sortNO = InputInt();
		for(i=1;i<=sortNO;i++){
			Printf("\nExecing the %dth sort....",i);
			Fork(sortTest);
		}
		break;		
		default:
		Printf("\nWrong input, please re-run the program.");
	}
	return 0;
}

void matTest(){
int A[Dim][Dim];
int B[Dim][Dim];
int C[Dim][Dim];

int i, j, k;

    for (i = 0; i < Dim; i++)		/* first initialize the matrices */
	for (j = 0; j < Dim; j++) {
	     A[i][j] = i;
	     B[i][j] = j;
	     C[i][j] = 0;
	}

    for (i = 0; i < Dim; i++)		/* then multiply them together */
	for (j = 0; j < Dim; j++)
            for (k = 0; k < Dim; k++)
		 C[i][j] += A[i][k] * B[k][j];
	Printf("The C[Dim-1][Dim-1] is %d\n",C[Dim-1][Dim-1]);
    Exit(0);		/* and then we're done */
}

void sortTest(){
	int A[1024];	/* size of physical memory; with code, we'll run out of space!*/
   int i, j, tmp;

    /* first initialize the array, in reverse sorted order */
    for (i = 0; i < 1024; i++)		
        A[i] = 1024 - i;

    /* then sort! */
    for (i = 0; i < 1023; i++)
        for (j = i; j < (1023 - i); j++)
	   if (A[j] > A[j + 1]) {	/* out of order -> need to swap ! */
	      tmp = A[j];
	      A[j] = A[j + 1];
	      A[j + 1] = tmp;
    	   }
	Printf("DEBUG: the A[0] is %d\n",A[0]);
    Exit(0);		/* and then we're done -- should be 0! */
}